﻿using System.Data.Entity;
using CapokunMvcTest.Models;
using System.Collections.Generic;

namespace CapokunMvcTest
{
	/// <summary>
	/// sql db context, conn string in web.config
	/// </summary>
	public class CapokunContext : DbContext
	{
		public DbSet<Song> Songs { get; set; }


		/// <summary>
		/// create song
		/// </summary>
		public static void CreateSong(Song newSong)
		{
			using (var db = new CapokunContext())
			{
				db.Songs.Add(newSong);
				db.SaveChanges();

			}
		}

		/// <summary>
		/// retrieve
		/// </summary>
		/// <returns></returns>
		public static Song RetrieveSong(string songId)
		{
			using (var db = new CapokunContext())
			{
				return db.Songs.Find(songId);
			}
		}

		/// <summary>
		/// modifies song in db with edited song params, songId is unchanged
		/// </summary>
		/// <param name="editedSong"></param>
		public static void UpdateSong(Song editedSong)
		{
			using (var db = new CapokunContext())
			{
				Song songInDb = db.Songs.Find(editedSong.SongId);
				songInDb.SongId_en = editedSong.SongId_en;
				songInDb.SongId_fr = editedSong.SongId_fr;
				songInDb.Link = editedSong.Link;
				songInDb.Content = editedSong.Content;
				songInDb.Content_fr = editedSong.Content_fr;
				songInDb.Content_en = editedSong.Content_en;
				songInDb.SubmittedBy = editedSong.SubmittedBy;


				db.Entry(songInDb).State = System.Data.EntityState.Modified;
				db.SaveChanges();

			}
		}

		public static void DeleteSong(string songId)
		{
			using (var db = new CapokunContext())
			{
				Song songInDb = db.Songs.Find(songId);
				db.Entry(songInDb).State = System.Data.EntityState.Deleted;
				db.SaveChanges();

			}
		}

		public static IEnumerable<Song> GetAllSongs()
		{
			var db = new CapokunContext();

			//db.Database.CreateIfNotExists();

			return db.Songs;
		}
	}
}





